package com.yxplearn.mybatisplus.mybatis_sample;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import org.apache.ibatis.executor.resultset.ResultSetHandler;
import org.apache.ibatis.plugin.*;
import org.springframework.util.ReflectionUtils;

import java.lang.reflect.Field;
import java.sql.Statement;
import java.util.List;
import java.util.Properties;

@Intercepts(
        @Signature(type = ResultSetHandler.class, method = "handleResultSets", args = {Statement.class})
)
public class CampHumpInterceptor implements Interceptor {

    private String name;

    public CampHumpInterceptor(String name) {
        this.name = name;
    }

    @Override
    public Object intercept(Invocation invocation) throws Throwable {
        System.out.println("intercept 执行前" + name);
        ResultSetHandler target = (ResultSetHandler) invocation.getTarget();

        Object[] args = invocation.getArgs();
        Statement statement = (Statement) args[0];
        List<Object> objects = target.handleResultSets(statement);
        for (Object object : objects) {
            Field[] fields = object.getClass().getDeclaredFields();
            for (Field field : fields) {
                field.setAccessible(true);
                if (field.getType().equals(String.class)) {
                    String fieldValue = (String) ReflectionUtils.getField(field, object);
                    field.set(object, fieldValue.toUpperCase());
                }
            }
        }
        System.out.println("intercept 执行前" + name);
        return objects;
    }

    @Override
    public Object plugin(Object target) {
        return Plugin.wrap(target, this);
    }

    @Override
    public void setProperties(Properties properties) {

    }



}
